package com.bestcem.xm.user.grpc.service;

import cn.hutool.core.lang.ObjectId;
import com.bestcem.bp.xm.user.grpc.v1.services.*;
import com.bestcem.xm.common.core.domain.web.RpcStatusResult;
import com.bestcem.xm.common.core.domain.web.ServiceResult;
import com.bestcem.xm.user.service.UnsubscribeSettingService;
import com.bestcem.xm.user.service.dto.setting.UnsubscribeSettingDTO;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 退订设置GRPC服务
 *
 * @author ympan <yuming.pan@idiaoyan.com>
 * @version v1.0
 * @date 2022/7/14 10:11
 */
@Slf4j
@GrpcService
public class UnsubscribeSettingGrpcService extends UnsubscribeSettingServiceGrpc.UnsubscribeSettingServiceImplBase {
    @Resource
    UnsubscribeSettingService unsubscribeSettingService;

    @Override
    public void getByOrgId(GetByOrgIdRequest request, StreamObserver<GetByOrgIdResponse> responseObserver) {
        GetByOrgIdResponse.Builder builder = GetByOrgIdResponse.newBuilder();
        String orgId = request.getOrgId();
        if (!ObjectId.isValid(orgId)) {
            builder.setStatus(RpcStatusResult.error("orgId格式错误"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        ServiceResult<UnsubscribeSettingDTO> result = unsubscribeSettingService.getByOrgId(orgId);
        if (!result.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        UnsubscribeSetting unsubscribeSetting = UnsubscribeSettingDTO.dto2Msg(result.getData());
        if (Objects.isNull(unsubscribeSetting)) {
            builder.setStatus(RpcStatusResult.success());
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        builder.setSetting(unsubscribeSetting);
        builder.setStatus(RpcStatusResult.success());
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }

    @Override
    public void genSig(GenSigRequest request, StreamObserver<GenSigResponse> responseObserver) {
        GenSigResponse.Builder builder = GenSigResponse.newBuilder();
        String orgId = request.getOrgId();
        if (!ObjectId.isValid(orgId)) {
            builder.setStatus(RpcStatusResult.error("orgId格式错误"));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        ServiceResult<String> result = unsubscribeSettingService.genSig(orgId);
        if (!result.isSuccess()) {
            builder.setStatus(RpcStatusResult.error(result.getMsg()));
            responseObserver.onNext(builder.build());
            responseObserver.onCompleted();
            return;
        }
        builder.setOrgId(orgId);
        builder.setSig(result.getData());
        builder.setStatus(RpcStatusResult.success());
        responseObserver.onNext(builder.build());
        responseObserver.onCompleted();
    }
}
